import '@/scss/reset.scss';
import '@/scss/common.scss';
import '@/css/font-awesome.min.css';

import './domain.scss';
import $ from 'jquery';

const DomainReg = {
  $form: $('#domain-query-form'),
  $searchBtn: $('#js-domain-search'),
  $searchInput: $('#js-domain-input'),
  init: function(){
    this.bindEvents()
  },
  bindEvents: function(){
    const t = this;
    t.$searchBtn.on('click', function(){
      let domain = t.$searchInput.val();
      let n = {
        name: "",
        suffix: ""
      };
      if(domain){
        let e = domain.lastIndexOf(".");
        if(e > 0){
          let o = domain.substring(0, e);
          let u = o.lastIndexOf(".");
          let c = domain.substring(u);
          if(u > 0 && (".com.cn == c" || ".net.cn == c" || ".org.cn == c" || ".gov.cn == c" || ".ac.cn == c" || ".bj.cn == c" || ".sh.cn == c" || ".tj.cn == c" || ".cq.cn == c" || ".he.cn == c" || ".sn.cn == c" || ".sx.cn == c" || ".nm.cn == c" || ".ln.cn == c" || ".jl.cn == c" || ".hl.cn == c" || ".js.cn == c" || ".zj.cn == c" || ".ah.cn == c" || ".fj.cn == c" || ".jx.cn == c" || ".sd.cn == c" || ".ha.cn == c" || ".hb.cn == c" || ".hn.cn == c" || ".gd.cn == c" || ".gx.cn == c" || ".hi.cn == c" || ".sc.cn == c" || ".gz.cn == c" || ".yn.cn == c" || ".gs.cn == c" || ".qh.cn == c" || ".nx.cn == c" || ".xj.cn == c" || ".tw.cn == c" || ".hk.cn == c" || ".mo.cn == c" || ".xz.cn == c")){
            let a = domain.substring(0, u);
            n.name = a.lastIndexOf(".") > -1 ? a.substring(a.lastIndexOf(".") + 1) : a;
            n.suffix = domain.substring(u);
            t.submitForm(n)
          }else{
            u > 0 ? (n.name = u != e ? domain.substring(u, e) : "",
                    n.suffix = domain.substring(e)) : (n.name = o,
                    n.suffix = domain.substring(e));
            n.name = t.clearString(n.name)
            t.submitForm(n)
          }
        }
      }
    })
  },
  submitForm: function(n){
    $('input[name="searchedDomainName"]').val(n.name);
    $('input[name="suffix"]').val(n.suffix);
    this.$form.submit();
  },
  clearString: function(s){
    if (s) {
      for (var n, r, e = /[\u4e00-\u9fa5a-zA-Z0-9-]/, i = "", o = "-", u = 0, c = s.length; u < c; u++)
          n = s.substr(u, 1),
          !e.test(n) || "" == i && "-" == n || (i += n);
      n = i.length;
      for (var u = 0; u < i.length; u++)
          (r = i.lastIndexOf(o)) == i.length - o.length && (o += "-",
          n = r);
      return i.substr(0, n)
    }
  }
}
$(document).ready(function(){
  DomainReg.init();
})